package com.scoressystem.controller;

import com.scoressystem.pojo.Teacher;
import com.scoressystem.service.TeacherService;
import com.scoressystem.utils.Results;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

  @Autowired  //去spring容器里根据类型获取一个TeacherService对象
   TeacherService teacherService;

  @RequestMapping("/login")
  @ResponseBody //返回值自动转json
    public Results login(@Param("tusername")String userName, @Param("tpass")String password, String userCode, HttpSession session) {
      String code=(String) session.getAttribute("systemCode");
      if(code.equalsIgnoreCase(userCode)) {
       Teacher teacher=  teacherService.selectTeacherlogin(userName,password);
       if(teacher!=null) {
      session.setAttribute("uname",teacher.getTusername());
           Results results = new Results("0","登录成功");
           return results;
       }
       else {
           Results results = new Results("1","登录失败");
           return results;
       }
      }
      else {
          Results results = new Results("3","验证码错误");
          return results;
      }
  }
    @RequestMapping("/updatePwd")
    @ResponseBody //返回值自动转json
    public Results updatePwd(String pwd ,String pwd1,String pwd2,HttpSession session) {
      String uname=(String) session.getAttribute("uname");
      Teacher teacher= teacherService.selectTeacherlogin(uname,pwd);
      if(teacher!=null) {
          if (pwd1.equalsIgnoreCase(pwd2)){
            Integer count1=  teacherService.updateTeacherpass( pwd1,uname);
            if (count1!=null){
                session.invalidate();
                return  new  Results("0","修改成功");
            }
            else {
                return  new  Results("3","修改失败");
            }
          }
          else {
              return new  Results("2","两次密码不相同");
          }
      }
      else {
          return  new Results("1","账户密码错误");
      }

    }
}
